package com.itheima.es;

import com.alibaba.fastjson.JSON;
import com.itheima.es.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.swing.text.Highlighter;
import java.io.IOException;
import java.util.Map;

/**
 * 演示: 操作es客户端
        高亮处理
 *
 * @作者 itcast
 * @创建日期 2021/3/19 9:09
 **/
public class EsDemo05 {
    RestHighLevelClient client;
    /**
     * note: 唱歌或javaee
     * match:
     * 年龄降序
     * @throws IOException
     */
    @Test
    public void highlight() throws IOException {
        // 1. 创建搜索请求
        SearchRequest request = new SearchRequest("user");
        // 2. 构建请求参数
        SearchSourceBuilder builder = new SearchSourceBuilder();
        // 2.1 查询条件
        builder.query(QueryBuilders.matchQuery("note","唱歌或javaee"));
        // 2.2 构建高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("note");//高亮字段
        highlightBuilder.preTags("<span>");//前置标签
        highlightBuilder.postTags("</span>");//后置标签
        builder.highlighter(highlightBuilder);
        request.source(builder);
        // 3. 执行搜索
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        for (SearchHit doc : hits) {
            User user = JSON.parseObject(doc.getSourceAsString(), User.class);
//            System.out.println(user); // 现在user的note字段并没有高亮处理
            // 得到高亮处理结果的map集合
            Map<String, HighlightField> highlightFields = doc.getHighlightFields();
            HighlightField highlightResult = highlightFields.get("note"); //note的高亮结果
            Text[] fragments = highlightResult.getFragments();// 获取高亮处理结果 片段数组
            // 将片段数组 拼接起来 就是完整的note字段高亮处理结果
            String note = StringUtils.join(fragments); // 工具类方法:将数组的每一个元素 拼接起来
            // 将高亮结果 覆盖user中的note字段
            user.setNote(note);
            System.out.println(user);
        }
    }

    @Before
    public void init(){
        // 创建操作es的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.129",9200,"http")
                )
        );
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        // 创建操作es的客户端
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("192.168.200.129",9200,"http")
//                )
//        );
//        // 使用client操作es
//
//        // 使用完毕后需要释放链接
//        try {
//            client.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
